// ==================== src/lib.rs ====================
//! Nacos核心抽象层
//! 
//! 本模块定义了整个Nacos系统的核心抽象，包括：
//! - 核心数据结构
//! - 服务接口trait
//! - 错误类型
//! - 元数据定义

pub mod error;
pub mod metadata;
pub mod traits;
pub mod config;
pub mod event;
pub mod auth;
pub mod tenant;

// 重新导出核心类型
pub use error::{NacosError, Result};
pub use metadata::*;
pub use traits::*;
pub use config::*;
pub use event::*;
pub use auth::*;
pub use tenant::*;

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_service_instance_new_and_get_key() {
        let instance = ServiceInstance::new(
            "test-service".to_string(),
            "test-group".to_string(),
            "127.0.0.1".to_string(),
            8080,
        );

        assert_eq!(instance.service_name, "test-service");
        assert_eq!(instance.group_name, "test-group");
        assert_eq!(instance.ip, "127.0.0.1");
        assert_eq!(instance.port, 8080);
        assert!(instance.healthy);
        assert!(instance.enabled);
        assert!(instance.ephemeral);
        assert_eq!(instance.weight, 1.0);

        let key = instance.get_key();
        let expected_key = format!("{}#{}#{}#{}", 
            instance.namespace, 
            instance.group_name, 
            instance.service_name, 
            instance.instance_id
        );
        assert_eq!(key, expected_key);
    }

    #[test]
    fn test_config_item_new_and_get_key() {
        let item = ConfigItem::new(
            "test-data-id".to_string(),
            "test-group".to_string(),
            "test-content".to_string(),
        );

        assert_eq!(item.data_id, "test-data-id");
        assert_eq!(item.group, "test-group");
        assert_eq!(item.content, "test-content");
        assert_eq!(item.version, 1);

        let key = item.get_key();
        let expected_key = format!("{}#{}#{}", 
            item.namespace, 
            item.group, 
            item.data_id
        );
        assert_eq!(key, expected_key);
    }

    #[test]
    fn test_app_config_default() {
        let config = AppConfig::default();

        assert_eq!(config.server.address, "0.0.0.0");
        assert_eq!(config.server.http_port, 8848);
        assert_eq!(config.cluster.cluster_name, "DEFAULT_CLUSTER");
        assert_eq!(config.storage.storage_type, StorageType::Memory);
        assert_eq!(config.network.connect_timeout, Duration::from_secs(10));
    }

    #[test]
    fn test_tenant_new() {
        let tenant = Tenant::new("test-tenant-id".to_string(), "test-tenant-name".to_string());
        assert_eq!(tenant.id, "test-tenant-id");
        assert_eq!(tenant.name, "test-tenant-name");
        assert!(tenant.description.is_none());
    }
}
